<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="950px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="主键ID" prop="id">
                    <el-input v-model="formData.id" placeholder="请输入主键ID" />
                </el-form-item>
                <el-form-item label="公开号" prop="publicationNumber">
                    <el-input v-model="formData.publicationNumber" placeholder="请输入公开号" />
                </el-form-item>
                <el-form-item label="合同编号" prop="contractNo">
                    <el-select
                        class="flex-1"
                        v-model="formData.contractNo"
                        placeholder="请选择合同编号"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="合同名称" prop="contractName">
                    <el-input v-model="formData.contractName" placeholder="请输入合同名称" />
                </el-form-item>
                <el-form-item label="客户文号" prop="customerDocumentNumber">
                    <el-input
                        v-model="formData.customerDocumentNumber"
                        placeholder="请输入客户文号"
                    />
                </el-form-item>
                <el-form-item label="申请人编号" prop="applicantId">
                    <el-input v-model="formData.applicantId" placeholder="请输入申请人编号" />
                </el-form-item>
                <el-form-item label="申请人" prop="applicant">
                    <el-input v-model="formData.applicant" placeholder="请输入申请人" />
                </el-form-item>
                <el-form-item label="我方文号" prop="ourDocumentNumber">
                    <el-input v-model="formData.ourDocumentNumber" placeholder="请输入我方文号" />
                </el-form-item>
                <el-form-item label="申请号" prop="applicationNumber">
                    <el-input v-model="formData.applicationNumber" placeholder="请输入申请号" />
                </el-form-item>
                <el-form-item label="申请日" prop="applicationDate">
                    <el-input v-model="formData.applicationDate" placeholder="请输入申请日" />
                </el-form-item>
                <el-form-item label="案件名称" prop="caseName">
                    <el-input v-model="formData.caseName" placeholder="请输入案件名称" />
                </el-form-item>
                <el-form-item label="案件类型编号" prop="caseTypeId">
                    <el-input v-model="formData.caseTypeId" placeholder="请输入案件类型编号" />
                </el-form-item>
                <el-form-item label="案件类型" prop="caseType">
                    <el-select
                        class="flex-1"
                        v-model="formData.caseType"
                        placeholder="请选择案件类型"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="案件状态编号" prop="caseStateId">
                    <el-input v-model="formData.caseStateId" placeholder="请输入案件状态编号" />
                </el-form-item>
                <el-form-item label="案件状态" prop="caseState">
                    <el-input v-model="formData.caseState" placeholder="请输入案件状态" />
                </el-form-item>
                <el-form-item label="业务类型编号" prop="businessTypeId">
                    <el-input v-model="formData.businessTypeId" placeholder="请输入业务类型编号" />
                </el-form-item>
                <el-form-item label="业务类型" prop="businessType">
                    <el-select
                        class="flex-1"
                        v-model="formData.businessType"
                        placeholder="请选择业务类型"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="申请类型编号" prop="applicationTypeId">
                    <el-input
                        v-model="formData.applicationTypeId"
                        placeholder="请输入申请类型编号"
                    />
                </el-form-item>
                <el-form-item label="申请类型" prop="applicationType">
                    <el-select
                        class="flex-1"
                        v-model="formData.applicationType"
                        placeholder="请选择申请类型"
                    >
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <el-form-item label="申请国家" prop="designatedCountry">
                    <el-input v-model="formData.designatedCountry" placeholder="请输入申请国家" />
                </el-form-item>
                <el-form-item label="第一联系人" prop="firstContact">
                    <el-input v-model="formData.firstContact" placeholder="请输入第一联系人" />
                </el-form-item>
                <el-form-item label="开卷日期" prop="openingDate">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.openingDate"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择开卷日期"
                    />
                </el-form-item>
                <el-form-item label="业务人员编号" prop="businessPersonnelId">
                    <el-input
                        v-model="formData.businessPersonnelId"
                        placeholder="请输入业务人员编号"
                    />
                </el-form-item>
                <el-form-item label="业务人员" prop="businessPersonnel">
                    <el-input v-model="formData.businessPersonnel" placeholder="请输入业务人员" />
                </el-form-item>
                <el-form-item label="原业务员编号" prop="originalSalesmanId">
                    <el-input
                        v-model="formData.originalSalesmanId"
                        placeholder="请输入原业务员编号"
                    />
                </el-form-item>
                <el-form-item label="原业务员" prop="originalSalesman">
                    <el-input v-model="formData.originalSalesman" placeholder="请输入原业务员" />
                </el-form-item>
                <el-form-item label="承办部门编号" prop="undertakeDepartmentId">
                    <el-input
                        v-model="formData.undertakeDepartmentId"
                        placeholder="请输入承办部门编号"
                    />
                </el-form-item>
                <el-form-item label="承办部门" prop="undertakeDepartment">
                    <el-input v-model="formData.undertakeDepartment" placeholder="请输入承办部门" />
                </el-form-item>
                <el-form-item label="流程人员编号" prop="processPersonnelId">
                    <el-input
                        v-model="formData.processPersonnelId"
                        placeholder="请输入流程人员编号"
                    />
                </el-form-item>
                <el-form-item label="流程人员" prop="processPersonnel">
                    <el-input v-model="formData.processPersonnel" placeholder="请输入流程人员" />
                </el-form-item>
            </el-form>
            <!-- <el-card class="!border-none" shadow="never">
        <el-form
          ref="formRef"
          label-width="138px"
          :model="queryParams"
          :inline="true"
        >
          <el-form-item label="案件类型" prop="caseType">
            <el-select
              class="w-[690px]"
              v-model="queryParams.caseType"
              clearable
            >
              <el-option label="专利" value="1" />
              <el-option label="商标" value="2" />
              <el-option label="版权" value="3" />
              <el-option label="科技服务" value="4" />
              <el-option label="法律服务" value="5" />
              <el-option label="调查案" value="6" />
            </el-select>
          </el-form-item>
          <el-form-item label="我方文号" prop="ourDocumentNumber">
            <el-input
              class="w-[690px]"
              v-model="queryParams.ourDocumentNumber"
            />
          </el-form-item>
          <el-form-item label="申请号" prop="applicationNumber">
            <el-input
              class="w-[690px]"
              v-model="queryParams.applicationNumber"
            />
          </el-form-item>
          <el-form-item style="float: right">
            <el-button type="primary" @click="resetPage">查询</el-button>
            <el-button type="primary" @click="resetParams">重置</el-button>
            <el-button type="primary" @click="resetParams">设置</el-button>
            <el-button type="primary" @click="resetParams">一键设置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card class="!border-none mt-4" shadow="never">
        <el-table
          class="mt-4"
          size="large"
          v-loading="pager.loading"
          :data="pager.lists"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column label="ID" prop="id" min-width="50" />
          <el-table-column
            label="我方文号"
            prop="ourDocumentNumber"
            min-width="100"
          />
          <el-table-column
            label="申请号"
            prop="applicationNumber"
            min-width="100"
          />
          <el-table-column label="案件名称" prop="caseName" min-width="100" />
          <el-table-column
            label="案件类型编号"
            prop="caseTypeId"
            min-width="100"
          />
          <el-table-column
            label="业务类型"
            prop="businessType"
            min-width="100"
          />
          <el-table-column
            label="申请类型"
            prop="applicationType"
            min-width="100"
          />
          <el-table-column
            label="申请国家"
            prop="designatedCountry"
            min-width="100"
          />
          <el-table-column
            label="业务人员"
            prop="businessPersonnel"
            min-width="100"
          />
        </el-table>
        <div class="flex justify-end mt-4">
          <pagination v-model="pager" @change="getLists" />
        </div>
      </el-card> -->
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { clientcaseEdit, clientcaseAdd, clientcaseDetail } from '@/api/clientcase'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑客户案件' : '新增客户案件'
})

const formData = reactive({
    id: '',
    publicationNumber: '',
    contractNo: '',
    contractName: '',
    customerDocumentNumber: '',
    applicantId: '',
    applicant: '',
    ourDocumentNumber: '',
    applicationNumber: '',
    applicationDate: '',
    caseName: '',
    caseTypeId: '',
    caseType: '',
    caseStateId: '',
    caseState: '',
    businessTypeId: '',
    businessType: '',
    applicationTypeId: '',
    applicationType: '',
    designatedCountry: '',
    firstContact: '',
    openingDate: '',
    businessPersonnelId: '',
    businessPersonnel: '',
    originalSalesmanId: '',
    originalSalesman: '',
    undertakeDepartmentId: '',
    undertakeDepartment: '',
    processPersonnelId: '',
    processPersonnel: ''
})

const formRules = {}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await clientcaseEdit(data) : await clientcaseAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await clientcaseDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})

import { clientcaseDelete, clientcaseLists } from '@/api/clientcase'
import { usePaging } from '@/hooks/usePaging'
const queryParams = reactive({
    publicationNumber: '',
    contractNo: '',
    customerDocumentNumber: '',
    applicant: '',
    ourDocumentNumber: '',
    applicationNumber: '',
    caseName: '',
    caseType: '',
    openingDateStart: '',
    openingDateEnd: ''
})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: clientcaseLists,
    params: queryParams
})
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await clientcaseDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
